<template>
    <div>
        <el-row>
            <el-col :span="24">
                <h1>电影管理</h1>
            </el-col>
        </el-row>
        <el-row v-if="!updateShow">
            <el-col>
                <el-button @click="handleList">刷新</el-button>
                <el-button @click="handleInsert">添加人员</el-button>
            </el-col>
            <el-col>
                <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
                    <el-button slot="append" icon="el-icon-search" @click="handlePage"></el-button>
                </el-input>
            </el-col>
            <el-col :span="24">
                <el-table
                        v-loading="loading"
                        :data="tableData"
                        style="width: 100%">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="名字"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="sort"
                            label="排序"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="pic"
                            label="图片"
                            width="180">
                      <template slot-scope="scope">
                        <el-image :src="scope.row.pic" :preview-src-list="[scope.row.pic]">
                          <div slot="error" class="image-slot">
                            <i class="el-icon-picture-outline"></i>
                          </div>
                        </el-image>
                      </template>
                    </el-table-column>
                    <el-table-column
                            prop="role"
                            label="演员/导演"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="insertTime"
                            label="添加时间"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="updateTime"
                            label="修改时间"
                            width="180">
                    </el-table-column>
                    <el-table-column  label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="handleUpdate(scope.$index)">修改</el-button>
                            <el-popconfirm
                                    confirm-button-text='好的'
                                    cancel-button-text='不用了'
                                    icon="el-icon-info"
                                    icon-color="red"
                                    title="这是一段内容确定删除吗？"
                                    @confirm="handleDelete(scope.$index)"
                            >
                                <el-button slot="reference" size="mini">删除</el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                        background
                        layout="prev, pager, next"
                        :total="total"
                        :page-size="pageSize"
                        @current-change="handlePage"
                        :current-page.sync="page">
                </el-pagination>
            </el-col>
        </el-row>
        <el-form v-if="updateShow" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="ruleForm.sort"></el-input>
            </el-form-item>
            <el-form-item label="图片" prop="pic">
              <el-upload
                  class="avatar-uploader"
                  action="http://localhost:8081/common/uploadPicture"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="角色" prop="role">
                <el-input v-model="ruleForm.role"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
                <el-button @click="handleInsert">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>


<script>
    export default {
        name: "ActorListView.vue",
        data() {
            return {
                imageUrl: void 0,
                loading: true,
                tableData: [],
                actor: {},
                updateShow: false,
                pageSize:5,
                page: 1,
                total:0,
                input: '',
                ruleForm: {
                    name: '',
                    sort: '',
                    pic: '',
                    role: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入人员名称', trigger: 'blur' },
                    ],
                    sort: [
                        { required: true, message: '请输入默认排序', trigger: 'blur' },
                    ],
                    pic: [
                        { required: true, message: '请上传图片', trigger: 'blur' }
                    ],
                    role: [
                        { required: true, message: '请输入角色', trigger: 'blur' }
                    ]
                }
            }
        },
        created() {
            this.handleList();
        },
        methods:{
            handleList(){
                this.axios.get("manage/actor/list").then(response=> {
                    if (response.data.code == 200) {
                        this.tableData = response.data.data.list;
                        this.total = response.data.total;
                    }
                }).finally(()=>this.loading=false)
            },
            handleUpdate(index){
                const data=this.tableData[index];
                this.ruleForm=data;
                this.updateShow=!this.updateShow;
            },
            handleInsert(){
                this.ruleForm={};
                this.updateShow=!this.updateShow;
            },
            handleDelete(index){
                this.axios.get("manage/actor/delete",{params:{id:this.tableData[index].id}}).then(response=>{
                    if (response.data.code==200){
                        this.tableData.splice(index,1);
                    }
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.axios.post("manage/actor/saveOrUpdate",this.ruleForm,{params:{}}).then(response=>{
                            this.updateShow=false;
                            this.ruleForm={};
                        })
                    } else {
                        console.log('错误填写');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handlePage(){
                this.axios.get("manage/actor/list",{params:{keyword:this.input,pageNum:this.page}}).then(response=>{
                    if (response.data.code){
                        this.tableData = response.data.data.list;
                        this.total = response.data.total;
                    }
                })
            },
            handleAvatarSuccess(res,file) {
              this.ruleForm.pic = res.msg;
              this.imageUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
              const isJPG = file.type === 'image/jpeg';
              const isLt2M = file.size / 1024 / 1024 < 2;

              if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
              }
              if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
              }
              return isJPG && isLt2M;
            }
        }
    }
</script>

<style scoped>
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
/deep/ .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>